home *** CD-ROM | disk | FTP | other *** search
/ Aminet 22 / Aminet 22 (1997)(GTI - Schatztruhe)[!][Dec 1997].iso / Aminet / dev / src / ConfigFileSrc.lha / ConfigFileSrc12 / ToggleFormat / ToggleFormat.c < prev   
Encoding:
C/C++ Source or Header  |  1997-10-02  |  3.3 KB  |  131 lines

  1. /*
  2. **  $PROJECT: ToggleFormat
  3. **
  4. **  $VER: ToggleFormat.c 2.5 (2.10.97)
  5. **
  6. **  (C) Copyright 1996-1997 Marcel Karas
  7. **      All Rights Reserved.
  8. **
  9. **  $HISTORY:
  10. **
  11. **  0.1  10.11.96 -- Inital first version.
  12. **  1.0  10.11.96 -- First bug free version.
  13. **  1.1  16.11.96 -- Changed Header->WBufLength to 16384.
  14. **  1.2  05.12.96 -- First public version.
  15. **  1.3  06.01.97 -- Changed Header->WBufLength to 32768.
  16. **  2.0  28.01.97 -- Needs and uses now ConfigFile.library V2.
  17. **                -- Opens a file now with cf_OpenPS() and a puddlesize 
  18. **                   of 32768 bytes.
  19. **  2.1  04.02.97 -- Recompiled with the new CF V2 header file.
  20. **  2.2  11.02.97 -- Added the new cf_Open() errorcodes.
  21. **  2.3  12.02.97 -- Removed the stuff Printf() call by an
  22. **                   OpenLibrary("dos.library", 36L) failure.
  23. **  2.4  16.02.97 -- Optimized code.
  24. **  2.5  02.10.97 -- Recompiled for public release.
  25. */
  26.  
  27. #include <exec/types.h>
  28. #include <exec/libraries.h>
  29. #include <dos/rdargs.h>
  30. #include <dos/dos.h>
  31.  
  32. #include <clib/exec_protos.h>
  33. #include <clib/dos_protos.h>
  34.  
  35. #include <pragmas/exec_sysbase_pragmas.h>
  36. #include <pragmas/dos_pragmas.h>
  37.  
  38. #include "OLibTagged.h"
  39.  
  40. #include <Libraries/ConfigFile.h>
  41. #include <CLib/ConfigFile_protos.h>
  42. #include <Pragmas/ConfigFile_pragmas.h>
  43.  
  44. struct Library *DOSBase;
  45. struct Library *SysBase;
  46. struct Library *CFBase;
  47.  
  48. enum { ARG_FILE ,ARG_MAX };
  49.  
  50. VOID PrintErr ( STRPTR );
  51.  
  52. ULONG start ( VOID )
  53. {
  54.     struct RDArgs *RDA;
  55.     LONG ArgAry[ARG_MAX] = { 0 };
  56.  
  57.     ULONG Result    = RETURN_ERROR, WMode;
  58.     ULONG Error        = 0;
  59.  
  60.     CFHeader *Header;
  61.  
  62.     if ( DOSBase = TaggedOpenLibrary (TLIB_DOS) )
  63.     {
  64.         if ( CFBase = OpenLibrary (CF_NAME, 2L) )
  65.         {
  66.             RDA = AllocDosObject (DOS_RDARGS, NULL);
  67.  
  68.             if ( ReadArgs ("ConfigFile/A", ArgAry, RDA) )
  69.             {
  70.                 Printf("Open and read file\n");
  71.  
  72.                 if ( Header = cf_Open ((STRPTR) ArgAry[ARG_FILE],
  73.                         CF_OMODE_OLDFILE | CF_OFLG_READ_TOO, &Error) )
  74.                 {
  75.                     Printf ("Write file\n");
  76.  
  77.                     WMode = ( Header->Flags & CF_HFLG_ASCII_FILE )
  78.                                     ? CF_WMODE_SHORT : CF_WMODE_ASCII;
  79.  
  80.                     if ( cf_Write (Header, WMode | CF_WFLG_WRITE_ALWAYS, &Error) )
  81.                     {
  82.                         Printf ("Ok\n");
  83.  
  84.                         Result = RETURN_OK;
  85.                     }
  86.                     else
  87.                     {
  88.                         switch ( Error )
  89.                         {
  90.                             case CF_WERR_ALLOC_WBUFFER:    PrintErr ("No memory for WriteBuffer"); break;
  91.                             default:                                PrintErr ("Unkown write failure"); break;
  92.                         }
  93.                     }
  94.  
  95.                     cf_Close (Header);
  96.                 }
  97.                 else
  98.                 {
  99.                     switch ( Error )
  100.                     {
  101.                         case CF_OERR_OPEN_FILE:        PrintErr ("Couldn't open CF file"); break;
  102.                         case CF_OERR_READ_FILE:        PrintErr ("Couldn't read CF file"); break;
  103.                         case CF_OERR_NO_FORMAT:        PrintErr ("File no CF format"); break;
  104.                         case CF_OERR_NO_SIZE:        PrintErr ("File has no size"); break;
  105.                         case CF_OERR_HEADER_MEM:    PrintErr ("No memory for Header"); break;
  106.  
  107.                         /* cf_Read() failures */
  108.                         case CF_RERR_FORMAT:            PrintErr ("File has an error in the format structure"); break;
  109.                         case CF_RERR_UNKOWN_ITYPE:    PrintErr ("An unkown item type was found"); break;
  110.  
  111.                         default:                            PrintErr ("Unkown open failure"); break;
  112.                     }
  113.                 }
  114.  
  115.                 FreeArgs (RDA);
  116.             }
  117.             else    PrintErr ("Wrong arguments");
  118.  
  119.             FreeDosObject (DOS_RDARGS, RDA);
  120.             CloseLibrary (CFBase);
  121.         }
  122.         else    PrintErr ("Couldn't open ConfigFile.library V2+");
  123.  
  124.         CloseLibrary (DOSBase);
  125.     }
  126.  
  127.     return (Result);
  128. }
  129.  
  130. VOID PrintErr ( STRPTR String ) { Printf ("ToggleFormat: %ls\n", String); }
  131.